package com.example.lemis.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lemis.common.exception.NotFoundException;
import com.example.lemis.common.exception.NotLoginException;
import com.example.lemis.common.utils.SessionUtils;
import com.example.lemis.model.domain.Admin;
import com.example.lemis.model.domain.Street;
import com.example.lemis.mapper.StreetMapper;
import com.example.lemis.model.dto.PageDTO;
import com.example.lemis.model.dto.StreetInsertDTO;
import com.example.lemis.model.dto.StreetQuery;
import com.example.lemis.model.vo.StreetVO;
import com.example.lemis.model.vo.PageResult;
import com.example.lemis.service.BaseCurdService;
import com.example.lemis.service.IStreetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 管理街道数据 服务实现类
 * </p>
 *
 * @author lxp
 * @since 2025-06-24
 */
@AllArgsConstructor
@Service
public class StreetServiceImpl extends ServiceImpl<StreetMapper, Street> implements IStreetService, BaseCurdService<Street> {
    private final StreetMapper streetMapper;

    @Override
    public StreetVO queryOne(StreetQuery dto) {
        Street entity = getOneByExample(dto);   // 一行搞定查询

        if (entity == null) {
            throw new NotFoundException("街道不存在");
        }

        StreetVO vo = new StreetVO();
        BeanUtils.copyProperties(entity, vo);   // 字段名 & 类型相同会自动复制
        return vo;
    }

    @Override
    public Page<StreetVO> queryPageWithDivision(StreetQuery dto, PageDTO pageInfo) {
        Page<StreetVO> page = new Page<>(pageInfo.getPageNo(), pageInfo.getPageSize());
        return streetMapper.queryPageWithDivision(dto, page);
    }

    @Override
    public int insert(Long divisionId, StreetInsertDTO streetRequest) {
        Admin admin = SessionUtils.getCurrentAdminInfo();
        if (admin == null) {
            throw new NotLoginException("当前用户未登录");
        }
        return streetMapper.insert(divisionId, admin.getId(), streetRequest);
    }
}
